<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Admin\LogHander;
use Illuminate\Http\Request;
use App\Http\Middleware;
use App\admin\Category;
use App\admin\Good;
use Intervention\Image\ImageManagerStatic as Image;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\DB;

class CategoryController extends BaseController
{
	public function __construct() {
		$this->middleware('admindo');
	}
	
	//登录页面
	public function index(Request $request)
	{
		$flight = Category::orderBy('id', 'desc')->paginate(10);
		return view('admin.category',['data' => $flight]);
	}
	
	public function add()
	{
		$cate0 = Category::orderBy('id', 'desc')->get();
		return view('admin.categoryadd',['cate0'=>$cate0]);
	}
	
//	public function addUpOne(Request $request){
//		$catName = $request->input('catName');
//		$catInfo = $request->input('catInfo');
//		if(!isset($catName) || !isset($catInfo)){
//			return "上传参数为空";
//		}
//		$Cate = new Category;
//		if ($request -> hasFile('fileOne')) {
//			$file = $request -> file('fileOne');
//			$name = date("Ymd").str_random(10) . '.jpg';
//			$path = public_path() .'/upload/category/'. $name;
//			Image::make($file) -> save($path);
//			$pathBase = 'upload/category/'. $name;
//			$Cate->imgurl = $pathBase;
//		}
//		$Cate->name = $catName;
//		$Cate->description = $catInfo;
//		$Cate->parent_id = 0;
//		$Cate->type = 0;
//		$Cate->state = 0;
//		
//		$str = json_encode($Cate);
//		if($Cate->save()){
//			$cars=array('name'=>'addUpOne','info'=>'顶级分类','cnt'=>$str);
//			$temp = new LogHander;
//			$temp->logs($cars);
//			return redirect('Categoryadd');
//		}
//		
//	}
	
	public function addUpTwo(Request $request){
		$catNameT = $request->input('catNameT');
		$catInfoT = $request->input('catInfoT');
		$catSelt = $request->input('selt');
		
		if(!isset($catNameT) || !isset($catInfoT)){
			return "上传参数为空";
		}
		$Cate = new Category;
		if ($request -> hasFile('fileTwo')) {
			$file = $request -> file('fileTwo');
			$entension = $file -> getClientOriginalExtension();
			$ents = array("jpg", "jpge", "png", "bmp","gif");
			if(!in_array($entension, $ents)){
				return "只支持jpg, jpge, png, bmp,gif类型的图片";
			}
			$name = date("Ymd").str_random(10) . '.'.$entension;
			$path = public_path() .'/upload/category/'. $name;
			Image::make($file) -> save($path);
			$pathBase = 'upload/category/'. $name;
			$Cate->imgurl = $pathBase;
		}else{
			return "请上传图片";
		}
		$Cate->name = $catNameT;
		$Cate->description = $catInfoT;
		$Cate->parent_id = $catSelt;
		$Cate->type = 1;
		$Cate->state = 0;
		$str = json_encode($Cate);
		if($Cate->save()){
			$cars=array('name'=>'addUpTwo','info'=>'分类','cnt'=>$str);
			$temp = new LogHander;
			$temp->logs($cars);
			return redirect('Categoryindex');
		}
	}
	
//	public function addUpThree(Request $request){
//		$catNameT = $request->input('catNameTH');
//		$catInfoT = $request->input('catInfoTH');
//		$catSelt = $request->input('seltThree');
//		
//		if(!isset($catNameT) || !isset($catInfoT)){
//			return "上传参数为空";
//		}
//		
//		$Cate = new Category;
//		if ($request -> hasFile('fileThree')) {
//			$file = $request -> file('fileThree');
//			$name = date("Ymd").str_random(10) . '.jpg';
//			$path = public_path() .'/upload/category/'. $name;
//			Image::make($file) -> save($path);
//			$pathBase = 'upload/category/'. $name;
//			$Cate->imgurl = $pathBase;
//		}
//		$Cate->name = $catNameT;
//		$Cate->description = $catInfoT;
//		$Cate->parent_id = $catSelt;
//		$Cate->type = 2;
//		$Cate->state = 0;
//		$str = json_encode($Cate);
//		if($Cate->save()){
//			$cars=array('name'=>'addUpThree','info'=>'三级分类','cnt'=>$str);
//			$temp = new LogHander;
//			$temp->logs($cars);
//			return redirect('Categoryadd');
//		}
//	}
	
	public function del(Request $request)
	{
		$id = $request->input('id');
		$cate = new Category;
		$data = $cate::find($id);
		
		$count = Good::where('type',$id)->count();
		if($count > 0){
			return "cannot";
		}
		if($data->delete()){
			return "ok";
		}else{
			return "no";
		}
	}
	
	public function edit(Request $request)
	{
		$id = $request->input('id');
		$cate = new Category;
		$temp = $cate::find($id);
		if(empty($temp)){
			return "没有此分类";
		}
		$cate0 = Category::orderBy('id', 'desc')->get();
		return view('admin.categoryedite',['data'=>$temp,'cate'=>$cate0]);
	}
	
	public function search()
	{
		return view('admin.login');
	}
	
	public function test(){
		$temps = Category::get();
		$condition=array();
		$arr = $this->getTreeClassList(3,$condition);
		dd($arr);
	}
	public function getTreeClassList($show_deep='3',$condition=array()){
        $class_list = Category::get();
        $goods_class = array();//分类数组
        echo '666';
        if(is_array($class_list) && !empty($class_list)) {
            $show_deep = intval($show_deep);
            if ($show_deep == 1){//只显示第一级时用循环给分类加上深度deep号码
                foreach ($class_list as $val) {
                    if($val['parent_id'] == 0) {
                        $val['deep'] = 1;
                        $goods_class[] = $val;
                    } else {
                        break;//父类编号不为0时退出循环
                    }
                }
            } else {//显示第二和三级时用递归
                $goods_class = $this->_getTreeClassList($show_deep,$class_list);
            }
			echo '777';
        }
        return $goods_class;
    }
	private function _getTreeClassList($show_deep,$class_list,$deep=1,$parent_id=0,$i=0){
        static $show_class = array();//树状的平行数组
        echo '888';
        if(is_array($class_list) && !empty($class_list)) {
            $size = count($class_list);
            if($i == 0) $show_class = array();//从0开始时清空数组，防止多次调用后出现重复
            for ($i;$i < $size;$i++) {//$i为上次循环到的分类编号，避免重新从第一条开始
                $val = $class_list[$i];
                $gc_id = $val['id'];
                $gc_parent_id = $val['parent_id'];
                if($gc_parent_id == $parent_id) {
                    $val['deep'] = $deep;
                    $show_class[] = $val;
                    if($deep < $show_deep && $deep < 3) {//本次深度小于显示深度时执行，避免取出的数据无用
                        $this->_getTreeClassList($show_deep,$class_list,$deep+1,$gc_id,$i+1);
                    }
                }
                if($gc_parent_id > $parent_id) break;//当前分类的父编号大于本次递归的时退出循环
            }
        }
        return $show_class;
    }
}
